<?php

namespace Budget\BudgetBundle\Controller;

use Budget\BudgetBundle\Entity\PlannedExpense;
use Budget\BudgetBundle\Entity\Expense;
use Budget\BudgetBundle\Form\PlannedExpenseType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Budget\BudgetBundle\Helper\DateHelper;

class PlannedExpenseController extends MainController
{
   
    public function newAction(Request $request) 
    {
        $user     = $this->getUser();
        $expense  = new PlannedExpense();
        $form     = $this->createForm(new PlannedExpenseType($user->getFamily()), $expense);
        $response = new \stdClass;
        $response->status = false;
       
        if ($request->getMethod() == "POST") {
             $form->bind($request);
            
            if ($form->isValid()) {
                $em = $this->getEntityManager();
                $expense->setUser($user);
                $expense->setCreatedBy($user);
                $em->persist($expense);
                $em->flush();
                $response->status = true;
            } 
        }
       
        return new JsonResponse($response);
    }
    
    public function getPlannedExpensesJsonAction(Request $request) 
    {
        $em   = $this->getEntityManager();
        $user = $this->getUser();
        $date = $request->query->get('date');
        
        //$expenses     =  $this->get('budget_expense')->getPlannedExpensesForFamilyToArray($user->getFamily(), $date);
        $expenses     =  $em->getRepository('BudgetBudgetBundle:PlannedExpense')->getPlannedExpensesForFamilyToArray($user->getFamily(), $date);
        $expensesJson =  $this->get('budget_expense')->getFormatPlannedExpensesToJson($expenses);
        $response     = new JsonResponse();
        $response->setData($expensesJson);
        return $response;
    }
    
    public function editAction(Request $request) 
    {
        $expenseId   = $request->request->get('id');
        $categoryId  = $request->request->get('category');
        $name        = $request->request->get('name');
        $dateExpired = $request->request->get('date_expired');
        $amount      = $request->request->get('amount');
        
        $user       = $this->getUser();
        $userFamily = $user->getFamily(); 
        $em         = $this->getEntityManager();
        $response   = new \stdClass;
        $response->status = true;
        
        $expense = $em->getRepository('BudgetBudgetBundle:PlannedExpense')->findByIdForFamily($userFamily, $expenseId);
        if (!$expense) {
            $response->status = false;
            return new JsonResponse($response);
        }
        
        $category = $em->getRepository('BudgetBudgetBundle:Category')->findByIdForFamily($user->getFamily(), $categoryId);
        if (!$category) {
            $response->status = false;
            return new JsonResponse($response);
        }
        
        $expense->setAmount($amount);
        $expense->setName($name);
        $expense->setCategory($category);
        $date = \DateTime::createFromFormat(DateHelper::FORMAT_USER, $dateExpired);
        $expense->setDateExpired($date);
        $em->persist($expense);
        $em->flush();
        
        return new JsonResponse($response);
    }
    
    public function deleteAction(Request $request) 
    {
        $id       = $request->request->get('id');
        $user     = $this->getUser();
        $em       = $this->getEntityManager();
        $expense  = $em->getRepository('BudgetBudgetBundle:PlannedExpense')->findByIdForFamily($user->getFamily(), $id);
        $response = new \stdClass;
        $response->status = true;
        
        if (!$expense) {
            $response->status = false;
            return new JsonResponse($response);
        }
        
        $expense->setDateDeleted(new \DateTime());
        $expense->setDeletedBy($user);
        $em->persist($expense);
        $em->flush();
      
        return new JsonResponse($response);
    }
    
    public function movieToExpensesAction(Request $request) 
    {
        $expenseId        = $request->request->get('id');
        $em               = $this->getEntityManager();
        $user             = $this->getUser();
        $expensePlanned   = $em->getRepository('BudgetBudgetBundle:PlannedExpense')->findByIdForFamily($user->getFamily(), $expenseId);
        $response         = new \stdClass;
        $response->status = true;
        $expense          = new Expense();  
        
        if (!$expensePlanned) {
            $response->message = 'Expense not found';
            $response->status = false;
            return new JsonResponse($response);
        }
    
        $expense->setUser($expensePlanned->getUser());
        $expense->setPrice($expensePlanned->getAmount());
        $expense->setComment($expensePlanned->getName());
        $expense->setDate(new \DateTime());
        $expense->setDateCreated($expensePlanned->getDateExpired());
        $expense->setCategory($expensePlanned->getCategory());
        $expense->setCreatedBy($expensePlanned->getCreatedBy());
        $em->persist($expense);
        
        $expensePlanned->setDateExecution(new \DateTime());
        $em->persist($expensePlanned);   
        
        $em->flush();

       
        return new JsonResponse($response);
    }
    
    public function copyExpensesAction(Request $request) 
    {
        $date         = $request->request->get('date');
        $user         = $this->getUser();
        $em           = $this->getEntityManager();
        $response     = new \stdClass;
        $monthPreview = DateHelper::getMonthPreview($date);
        $response->status = true;

        //$expensesPlanned = $this->get('budget_expense')->getPlannedExpensesForFamily($user->getFamily(), $monthPreview);
        $expensesPlanned = $em->getRepository('BudgetBudgetBundle:PlannedExpense')->getPlannedExpensesForFamily($user->getFamily(), $monthPreview);
        if (!$expensesPlanned) {
            $response->status = false;
            return new JsonResponse($response);
        }
        
        foreach($expensesPlanned AS $expense) {
            $plannedExpense = new PlannedExpense(); 
            $plannedExpense->setAmount($expense->getAmount());
            $plannedExpense->setName($expense->getName());
            $plannedExpense->setCategory($expense->getCategory());
            $plannedExpense->setUser($expense->getUser());
            $plannedExpense->setDateExpired($expense->getDateExpired()->modify('+1 month'));
            $em->persist($plannedExpense);
        }
        $em->flush();
        
        $expense = new Expense();  
       
        return new JsonResponse($response);
    }
}